/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ugent.medirem.logic;

import be.ugent.medirem.persistence.entity.details.PrescriptionDetails;
import be.ugent.medirem.persistence.entity.values.MedicationTaskValues;
import be.ugent.medirem.persistence.entity.values.PrescriptionValues;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import be.ugent.medirem.persistence.facade.PrescriptionDaoLocal;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Thomas Roelens
 */
@Stateless
public class PrescriptionBean implements PrescriptionRemote {

    @EJB
    private TaskRemote taskBean;
    @EJB
    private PrescriptionDaoLocal prescriptionDaoBean;

    @RolesAllowed("gp")
    public void prescribeMedication(Long patientId, PrescriptionValues details) {
        Long prescrId = prescriptionDaoBean.create(details);
        prescriptionDaoBean.addPrescription(patientId, prescrId);

        // Make task
        taskBean.createTask(new MedicationTaskValues(prescrId, details.getStartTime()));
    }

    @RolesAllowed({"gp", "patient"})
    public List<PrescriptionDetails> getPrescriptions(Long patientId) {
        return prescriptionDaoBean.getPrescriptions(patientId);
    }

    @RolesAllowed("gp")
    public void removePrescriptionFromPatient(Long patientId, Long prescriptionId) {
        prescriptionDaoBean.removePrescriptionFromPatient(patientId, prescriptionId);
    }

    @RolesAllowed("gp")
    public void removePrescription(Long id) throws EntityNotFoundException, IllegalArgumentException {
        prescriptionDaoBean.remove(id);
    }

    @RolesAllowed({"gp", "patient"})
    public PrescriptionDetails getPrescriptionDetails(Long prescriptionId) throws EntityNotFoundException, IllegalArgumentException {
        return prescriptionDaoBean.getDetails(prescriptionId);
    }
}
